/*
 * Copyright (c) 2022-2023 cetcclout Co., Ltd All rights reserved.
 * Sensor Adapter is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.cetccloud.cimp.common.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @apiNote 信息封装的统一返回对象
 * @date 2022/12/23
 */
public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    private static final int INTERNAL_SERVER_ERROR = 500;

    /**
     * 默认构造方法成功信息
     */
    public R() {
        //操作是否成功
        put("success", true);
        //操作代码
        put("code", 0);
        //操作提示信息
        put("msg", "success");
    }

    /**
     * 默认失败信息
     */
    public static R error() {
        return error(INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }

    /**
     * 自定义错误信息
     */
    public static R error(String msg) {
        return error(INTERNAL_SERVER_ERROR, msg);
    }

    /**
     * 自定义错误码、错误信息
     */
    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        r.put("success", false);
        return r;
    }

    /**
     * 自定义成功信息
     */
    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    /**
     * 自定义成功码、成功信息
     */
    public static R ok(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        r.put("success", true);
        return r;
    }

    /**
     * 自定义成功信息体
     */
    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    /**
     * 默认成功信息
     */
    public static R ok() {
        return new R();
    }
    
    /**
     * 设置消息体属性信息
     */
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
